import { defineStore } from 'pinia';
import {familyTree,menuTree} from "@/utils/index";
import router from "@/router";
const Layout = () => import('@/layout/index.vue');

const modules = import.meta.glob('../../views/**/**.vue');
export const useAppStore = defineStore({
  id: 'app',
  state: () => {
    return {
      login:{
        username:'',
        password:'',
        record:false
      },
      routeHasMounted:true,// true  需要加载接口
      isMobile:false,
      token:'',
      refreshToken:'',
      isCollapse: false,
      isChat: false,
      menuActive: '',
      nav:[],
      menuList:[],
      breadcrumb:[],
      tableHeight:document.documentElement.clientHeight*0.66
    }
  },
  getters:{ // 相当于vue里面的计算属性，可以缓存数据

  },
  actions:{
    addRecord(event:Object){
      this.login=event;
    },
    addRouteHas(event:Boolean){
      this.routeHasMounted=event;
    },
    addIsMobile(isMobile:Boolean){
      this.isMobile = isMobile;
    },
    addChat(isChat:Boolean){
      this.isChat = isChat;
    },
    addCollapse(isCollapse:Boolean) {
      this.isCollapse = isCollapse;
    },
    addNav(nav:any) {
        if(this.nav.length>0){
            let i=-1;
            this.nav.forEach((element:any,index:number) => {
                if(element.menuId.toString()==nav.menuId){
                    i=index;
                }
            });
            if(i==-1){
              this.nav.push(nav);
            }
        }else{
          this.nav.push(nav);
        }
    },
    addMenuActive(menuId:String) {
      this.breadcrumb=familyTree(this.menuList,menuId);
      this.menuActive = menuId;
    },
    addTabRemove(menuId:String) {
      this.nav = this.nav.filter((tab:any) => tab.menuId.toString() != menuId);
    },
    addMenuList(menuList:Object){
      this.menuList = menuList;
    },
    addToken(token:String){
      this.token=token;
    },
    outIn(){
      this.token='';
      this.isCollapse=false;
      this.menuActive='';
      this.nav=[];
      this.menuList=[];
      this.breadcrumb=[];
    },
    getAddNewRoute(){
      // console.info(router);
      return new Promise((resolve)=>{
        let NewMenusList=menuTree(this.menuList);
        let newRoute : Array<any> = [];
        NewMenusList.forEach((item:any)=>{
            newRoute.push({
                name: item.menuId,
                meta: {title: item.name,menu: item.menu},
                path: item.path,
                url:item.url?item.url:'404',
                component:modules[`../../views${item.url}`] ? modules[`../../views${item.url}`] : modules[`../../views/common/404.vue`]
            })
        })
        // this.addMenuActive('999999');
        // this.addNav({name:"控制台",menuId:'999999',path:'/dashboard'});
        router.addRoute(
            {
                path: "/dashboard",
                component: Layout,
                children: [
                    ...newRoute,
                    // {
                    //   parentMenuId: null,
                    //   path: '/dashboard',
                    //   url: '/dashboard',
                    //   component:modules[`../../views/dashboard/index.vue`],
                    //   name: '控制台',
                    //   icon: 'icon-menu',
                    //   menuId: 999999,
                    //   menu: true,
                    // },
                    {
                      name: "99998",
                      meta: {title: "refresh",menu: "99998"},
                      path: "/refresh",
                      url:"refresh",
                      component:modules[`../../views/common/refresh.vue`] ? modules[`../../views/common/refresh.vue`] : modules[`../../views/common/404.vue`]
                    }
                ]
            }
        );
        resolve(true);
      })
    }
  },
  persist: {
    storage: localStorage
  }
})
// const modules = import.meta.glob('../../views/**');